import advListView from '$v/advList'
import {
  addAdvRequest,
  findAdvRequest,
  deleteAdvRequest,
  updateAdvRequest
} from '../api/addAdv';
import {
  isLoginRequest
} from '../api/admin'

//封装渲广告列表函数
const showAdvList = async function (res,pageNo,everyNum) {
  try {
    const findResult = await findAdvRequest(pageNo,everyNum)
    res.render(advListView({
      advList: findResult.data.findResult,
      allPageCount:findResult.data.allPageCount
    }))
  } catch (e) {
    alert("查询广告列表错误" + e.message)
    判断是否是权限异常
    if (!e.isLogin) {
      return router.go("/login");
    }
  }
}

export default async (req, res) => {



  try {
    await isLoginRequest();
  } catch (e) {
    //请求异常后提示用户
    alert("有异常" + e.message)
    //判断是否是权限异常
    if (!e.isLogin) {
      return router.go("/login");
    }
  }

  // 渲染广告列表页面
  showAdvList(res)




  //广告的增删改查
  document.onclick = async function (e) {

    //添加
    if (e.target.id === "adv-save") {
      e.preventDefault()
      //拿到表单数据
      const myFrom = new FormData(document.advForm)
      // console.log(myFrom.get("advTitle"));
      // console.log(myFrom.get("advType"));
      // console.log(myFrom.get("advOrder"));
      // console.log(myFrom.get("advPic"));
      //发送添加请求
      if (!myFrom.get("advTitle") || !myFrom.get("advType") || !myFrom.get("advOrder") || !myFrom.get("advPic").name) {
        return alert('请书写完整')
      }
      try {
        await addAdvRequest(myFrom)
        document.querySelector("#advClose").click()
        document.advForm.reset()
        alert("添加成功")
        //渲染页面
        showAdvList(res)
      } catch (e) {
        alert("添加广告列表异常" + e.message)
        //判断是否是权限异常
        if (!e.isLogin) {
          return router.go("/login");
        }
      }
    }


    //删除广告

    if (e.target.nodeName === "BUTTON" && e.target.classList.contains("btn-delete")) {
      const _id = e.target.dataset.id
      try {
        await deleteAdvRequest(_id)
        alert("删除成功")
        // 渲染广告列表页面
        showAdvList(res)
      } catch (e) {
        alert("删除广告失败" + e.message)
      }
    }

    //更新广告表单渲染
    if (e.target.nodeName === "BUTTON" && e.target.classList.contains("btn-update")) {
      //不用发请求渲染 但是没有_id的值 后续无法发送更新请求所需要的参数
      // //拿到所有广告列表
      // const oList = document.querySelectorAll("tbody>.advList")
      // //获取是第几个更新按钮
      // const id = e.target.dataset.id
      // document.updateForm.advTitle.value = oList[id].querySelectorAll("td")[1].innerHTML
      // document.updateForm.advType.value = oList[id].querySelectorAll("td")[2].innerHTML
      // document.updateForm.advOrder.value = oList[id].querySelectorAll("td")[5].innerHTML



      try {
        const findResult = await findAdvRequest()
        const {
          data
        } = findResult
        //获取是第几个更新按钮
        const id = e.target.dataset.id
        const updateFrom = document.updateForm
        updateFrom.advTitle.value = data[id].advTitle
        updateFrom.advType.value = data[id].advType
        updateFrom.advOrder.value = data[id].advOrder

        //给更新按钮添加_id,发送更新请求所需要的参数_id
        document.querySelector("#update-save").dataset._id = data[id]._id
      } catch (error) {
        alert("更新表单加载异常" + e.message)
      }
    }



    //更新表单
    if (e.target.id === "update-save") {

      //拿到表单数据
      const myFrom = new FormData(document.updateForm)
      if (!myFrom.get("advTitle") || !myFrom.get("advType") || !myFrom.get("advOrder") || !myFrom.get("advPic").name) {
        return alert('请书写完整')
      }
      // 获取参数_id
      const _id = e.target.dataset._id
      try {
        await updateAdvRequest(_id, myFrom)
        document.querySelector("#updateClose").click
        document.querySelector(".modal-backdrop").style.display="none"
        // 渲染广告列表页面
        showAdvList(res)
      } catch (e) {
        alert("添加广告失败" + e.message)
      }

    }
       //点击页码更新列表
  if(e.target.nodeName==="SPAN"&&e.target.parentNode.classList.contains("page")){
   const pageno= e.target.dataset.pageno
   try {
    await showAdvList(res,pageno)
   } catch (e) {
     alert("翻页获取列表失败"+e.message)
   }
    
  }

  }


  //表单上传数据预览
  document.onchange = function (e) {
    const oPre = document.querySelector("#preImg")
    if (e.target.id === "advPic") {
      const reader = new FileReader()
      reader.readAsDataURL(e.target.files[0]);
      //当某个reader读完以后,会触发onload事件
      reader.onload = function (e) {
        //e.target.result就是最后读取到的数据

        oPre.src = e.target.result;
        oPre.style.display = "block"
      }
    }

  }

 
}